package suncertify.ui;

import java.io.IOException;
import java.io.Serializable;
import java.util.Vector;

import javax.swing.table.AbstractTableModel;

import suncertify.db.Data;
import suncertify.db.Room;
import suncertify.network.ServerHelper;
import suncertify.network.ServerInterface;
import suncertify.resources.App;

/**
 * A custom <code>AbstractTableModel</code> to be used by a JTable object
 * @author Paul Wilhelm
 *
 */
public class ResultsTableModel extends AbstractTableModel implements Serializable{

	private static final long serialVersionUID = 1981;			//A UID to be used during deserialization
	private Vector<Room> rows;				//A Vector of Rooms that will be the rows of the table
	private String[] columns;				//A String[] that will represent the column names
	private ServerInterface data;			//The data object to be used.
	
	/**
	 * Constructor.  Creates a new <code>ResultsTableModel</code> object to be used by a JTable
	 */
	public ResultsTableModel(){
		if (App.isNetworked){			//If in networked mode
			data = ServerHelper.getRemoteDatabase();
		}
		else {					//If in alone mode
			data = Data.getInstance();
		}
		try {
			rows = data.getAllRooms();
			columns = SearchGui.COLUMN_NAMES;
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Constructor.  Creates a new ResultsTableModel</code> object to be used by a JTable
	 * @param rows - The rows to be used by the tableModel
	 * @param columns - The columns to be used by the tableModel
	 */
	public ResultsTableModel(Vector<Room> rows, String[] columns){
		this.rows = rows;
		this.columns = columns;
	}

	/**
	 * {@inheritDoc}
	 */
	public int getColumnCount() {
		return 7;
	}

	/**
	 * {@inheritDoc}
	 */
	public int getRowCount() {
		return rows.size();
	}

	/**
	 * {@inheritDoc}
	 */
	public Object getValueAt(int rowIndex, int columnIndex) {
		Room room = rows.get(rowIndex);
		switch (columnIndex){
			case 0:
				return room.getName();
			case 1:
				return room.getLocation();
			case 2:
				return room.getSize();
			case 3:
				return room.getSmoking();
			case 4:
				return room.getRate();
			case 5:
				if (room.getOwner().equals("")){
					return "Yes";
				}
				return "No";
			case 6:
				return room.getDate();
		}
		return null;
	}
	
	/**
	 * {@inheritDoc}
	 */
	public String getColumnName(int index){
		return columns[index];
	}
	
	/**
	 * {@inheritDoc}
	 */
	public boolean isCellEditable(int row, int col){
		return false;
	}
	
	
	
	
}
